#include "chnwidget.h"
#include "readonlydelegate.h"

extern Channel chn[2];

ChnWidget::ChnWidget(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);
    chnModel1=new ChnStandardItemModel;
    chnModel2=new ChnStandardItemModel;
    chnView1->setModel(chnModel1);
    chnView2->setModel(chnModel2);
    chnView1->setItemDelegateForColumn(0,new readonlyDelegate(this));
    chnView1->setItemDelegateForColumn(1,new readonlyDelegate(this));
    chnView2->setItemDelegateForColumn(0,new readonlyDelegate(this));
    chnView2->setItemDelegateForColumn(1,new readonlyDelegate(this));

    connect(backBtn,SIGNAL(clicked()),this,SLOT(back()));
    connect(nextBtn,SIGNAL(clicked()),this,SLOT(next()));
    connect(chnModel1,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(devNumChanged1(QModelIndex,QModelIndex)));
    connect(chnModel2,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(devNumChanged2(QModelIndex,QModelIndex)));


}

void ChnWidget::refreshData()
{
    chnModel1->first=false;
    chnModel2->first=false;
    chnModel1->setDatas(chn[0].sysdev);
    chnModel2->setDatas(chn[1].sysdev);

}


void ChnWidget:: devNumChanged1(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    int row=topLeft.row();
    int column=topLeft.column();
    QVariant val=topLeft.data();

    if(column==2)
    {
        chn[0].sysdev[row].devAddr=val.toInt();
    }

}

void ChnWidget:: devNumChanged2(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    int row=topLeft.row();
    int column=topLeft.column();
    QVariant val=topLeft.data();

    if(column==2)
    {
        chn[1].sysdev[row].devAddr=val.toInt();
    }

}

void ChnWidget::next()
{
    chn[0].fac_no=0;
    chn[0].com_name=com1Text->text();
    chn[0].baud=comboBox1->currentText().toInt();


    chn[1].fac_no=1;
    chn[1].com_name=com2Text->text();
    chn[1].baud=comboBox2->currentText().toInt();

    emit mystep(1);

}

void ChnWidget::back()
{
    emit mystep(-1);
}







ChnStandardItemModel::ChnStandardItemModel(QObject *parent):QStandardItemModel(parent)
{
    this->initProperty();
    //this->setData();
    first=true;
}

ChnStandardItemModel::~ChnStandardItemModel()
{

}

void ChnStandardItemModel::initProperty()
{
    this->setColumnCount(3);
    QStringList list;
    list<<"装置类型"<<"装置名称"<<"地址";
    this->setHorizontalHeaderLabels( list);
}

QVariant ChnStandardItemModel::data(const QModelIndex & index,int role) const
{
    int column=index.column();
    int row=index.row();

    if(role==Qt::DisplayRole)
    {
        if(column==0)
            return datas.at(row).dev_type;
        if(column==1)
            return datas.at(row).dev_name;
        if(column==2)
        {
            if(datas.at(row).devAddr!=0)
            {
                return datas.at(row).devAddr;
            }
        }
    }


    return QStandardItemModel::data(index,role);
}

void ChnStandardItemModel::setDatas(QList<DEV> datas)
{
//    this->clear();
    this->removeRows(0,this->rowCount());
    this->setRowCount(datas.size());
    this->datas=datas;
    reset();
}



